<template>
  <!-- 订单执行状况 -->
  <div>
    <FormPanel ref="topForm" :list="topFilterList" :cols="3" :labelWidth="85" :rows="3" :isSubmitBtn="false" />
    <div class="searchBtn">
      <el-button type="primary" @click="handleLocalQuery">本地查询</el-button>
      <el-button type="primary" @click="handleRealeTimeQuery">实时查询</el-button>
      <el-button type="primary" @click="handleReset">重置</el-button>
    </div>
    <FilterTable
      ref="table"
      :columns="tableColumns"
      columnsRef="partPartPurchasePurchaseOrderMainOrderExecutiveCondition"
      selectionType="single"
      :fetchapi="fetchapi"
      :params="fetchParams"
      :onColumnsChange="columns => (this.tableColumns = columns)"
    >
      <template slot="controls">
        <!-- <el-button size="small" type="primary" @click="handleAOGConfirm">到货确认</el-button> -->
        <el-button size="small" type="primary" @click="handleRequestNotePrint">申请单打印</el-button>
        <el-button size="small" type="primary" @click="handleDispatchBillPrint">发货单打印</el-button>
        <!-- <el-button size="small" type="primary">导出</el-button>
        <el-button size="small" type="primary">打印</el-button>-->
      </template>
    </FilterTable>

    <div class="btnWrap">
      <el-button @click="handleCancle">取消</el-button>
      <el-button type="primary">确认</el-button>
    </div>

    <!-- 到货确认痰喘 -->
    <BaseDialog :visible.sync="confirmAOGVisible" title="到货确认" :destroyOnClose="true" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <confirmAOG @close="confirmAOGVisible = false"></confirmAOG>
    </BaseDialog>
    <!-- 申请单打印 -->
    <div v-if="printSHow">
      <BasePrint ref="print1" :data="requestNotePrintData" template="part/partPurchase/purchaseOrder/templateRequestNotePrint" />
    </div>
    <!-- 发货单打印 -->
    <div v-if="printtwoSHow">
      <BasePrint ref="print2" :data="dispatchBillPrintData" template="part/partPurchase/purchaseOrder/templateDispatchBillPrint" />
    </div>
  </div>
</template>

<script>
import { queryOrderExecuteStatus, queryOrderExecuteStatusRealTime, queryAllOrderTypeInfo } from '@/api/part/partPurchase/purchaseOrder';

import confirmAOG from './confirmAOG';

export default {
  components: {
    confirmAOG
  },
  data() {
    return {
      printSHow: false,
      printtwoSHow: false,
      topFilterList: this.createTopFilterList(),
      tableColumns: this.createTableColumns(),
      confirmAOGVisible: false, // 是否显示到货确认弹窗
      fetchapi: queryOrderExecuteStatus,
      fetchParams: { xhrAbort: false },
      orderTypeData: {}, // 订单类型数据(翻译雍)
      requestNotePrintData: {}, // 申请单打印数据
      dispatchBillPrintData: {} // 发货单打印数据
    };
  },
  created() {
    this.queryOrderTypeList();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '订货申请编号',
          fieldName: 'vbeln'
        },
        {
          type: 'RANGE_DATE',
          label: '订货申请日期',
          fieldName: 'orderDateBefore|orderDateAfter'
        },
        {
          type: 'INPUT',
          label: '发货清单号',
          fieldName: 'dvbeln'
        },
        {
          type: 'RANGE_DATE',
          label: '发货清单日期',
          fieldName: 'sendDateBefore|sendDateAfter'
        },
        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'svbeln'
        },
        {
          type: 'RANGE_DATE',
          label: '销售订单日期',
          fieldName: 'saleDateBefore|saleDateAfter'
        },
        {
          type: 'INPUT',
          label: '采购订单号',
          fieldName: 'orderNo'
        },
        // {
        //   type: 'SELECT',
        //   label: '订单性质',
        //   fieldName: 'properties',
        //   itemList: [{ text: 1, value: 1 }, { text: 2, value: 2 }]
        // },
        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          itemList: []
        },
        {
          type: 'INPUT',
          fieldName: 'partNo',
          label: '配件代码'
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '订货申请单号',
          dataIndex: 'vbeln',
          filter: true,
          filterType: 'input'
        },
        {
          title: '有效期天数',
          dataIndex: 'validday',
          filter: true,
          filterType: 'number'
        },
        {
          title: '建立人',
          dataIndex: 'ernam',
          filter: true,
          filterType: 'input'
        },
        {
          title: '建立日期',
          dataIndex: 'erdat',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '需求单类型',
          dataIndex: 'auart',
          filter: true,
          filterType: 'input'
        },
        {
          title: '确认',
          dataIndex: 'zbsta',
          filter: true,
          filterType: 'input'
        },
        {
          title: '紧急',
          dataIndex: 'zflag',
          filter: true,
          filterType: 'input'
        },
        {
          title: '发货单建立日期',
          dataIndex: 'ferdat',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '确认用户',
          dataIndex: 'spnam',
          filter: true,
          filterType: 'input'
        },
        {
          title: '确认日期',
          dataIndex: 'spdat',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '确认时间',
          dataIndex: 'sptim',
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '销售订单号',
          dataIndex: 'vorderno',
          filter: true,
          filterType: 'input'
        }
      ];
    },
    //  查询所有订单类型
    async queryOrderTypeList() {
      let res = await queryAllOrderTypeInfo();
      let obj = {};
      this.topFilterList.find(item => item.fieldName == 'orderType').itemList = res.data.map(item => {
        obj[item.partTypeNo] = item.partTypeName;
        return { value: item.partTypeId, text: item.partTypeName };
      });
      this.orderTypeData = obj;
    },
    // 点击本地查询
    async handleLocalQuery() {
      // this.fetchParams.xhrAbort = true;
      this.fetchapi = queryOrderExecuteStatus;
      this.reSearchTableData();
    },
    // 点击实时查询
    async handleRealeTimeQuery() {
      // this.fetchParams.xhrAbort = true;
      this.fetchapi = queryOrderExecuteStatusRealTime;
      this.reSearchTableData();
    },
    // 点击重试
    async handleReset() {
      this.$refs.topForm.RESET_FORM();
      this.reSearchTableData();
    },
    // 重新查询主表数据
    async reSearchTableData() {
      let formData = await this.$refs.topForm.GET_FORM_DATA();
      this.fetchParams.xhrAbort = true;
      this.fetchParams = Object.assign(this.fetchParams, {
        xhrAbort: false,
        ...formData[1]
      });
    },
    // 点击申请单打印
    handleRequestNotePrint() {
      this.printSHow = true;
      this.$refs.print1.EXCUTE_PRINT();
    },
    // 点击发货单打印
    handleDispatchBillPrint() {
      this.printtwoSHow = true;
      // 发货单打印假数据
      this.dispatchBillPrintData = {
        data1: '11111',
        data2: '22222',
        data3: '33333'
      };
      this.$nextTick(() => {
        this.$refs.print2.EXCUTE_PRINT();
      });
    },
    // 关闭弹窗
    handleCancle() {
      this.$emit('close');
    },
    // 点击到货确认
    handleAOGConfirm() {
      this.confirmAOGVisible = true;
    }
  }
};
</script>

<style scoped>
.btnWrap {
  position: absolute;
  left: 0px;
  bottom: 0px;
  right: 0px;
  z-index: 9;
  border-top: 1px solid rgb(233, 233, 233);
  padding: 10px 20px;
  background: rgb(255, 255, 255);
  text-align: right;
}
.searchBtn {
  text-align: center;
  margin-bottom: 15px;
}
</style>
